package Handler;
import Service.IProductService;
import Service.IShopCartService;
import Service.impl.ProductServiceImpl;
import Service.impl.ShopCartServiceImpl;
import dao.ICartEntryDao;
import dao.impl.CartEntryDaoImpl;
import pojo.CartEntry;
import pojo.Product;
import pojo.ShopCart;
import pojo.User;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ShopCartHandler {
    public void addToCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            if(request.getSession().getAttribute("user")!=null)
            {
                User user= (User) request.getSession().getAttribute("user");
                String id = request.getParameter("productid");
                int productId  = Integer.parseInt(id);
                IProductService productService=new ProductServiceImpl();
                Product product=productService.findById(productId);//获取产品
                ICartEntryDao cartEntryDao=new CartEntryDaoImpl();
                CartEntry cartEntry=new CartEntry();
                cartEntry.setProduct(product);
                ShopCart shopCart=new ShopCart();
                shopCart.setUser(user);
                cartEntry.setShopCart(shopCart);
                cartEntry.setCount(1);
                cartEntryDao.addCartEntry(cartEntry);
                request.getRequestDispatcher("/cart/showCart.do").forward(request,response);
            }
            else
            {
                request.getRequestDispatcher("/user/login.jsp").forward(request,response);
            }

        }catch (Exception e){
            e.printStackTrace();
            request.setAttribute("message","加入购物车失败!!!!");
            request.getRequestDispatcher("/shop/error.jsp").forward(request,response);
        }
    }
    public void showCartByUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(request.getSession().getAttribute("user")!=null){
            ICartEntryDao cartEntryDao=new CartEntryDaoImpl();
            List<CartEntry> cartEntryList=cartEntryDao.findByShopCartId((User) request.getSession().getAttribute("user"));
            request.setAttribute("cartEntryList",cartEntryList);
            request.getRequestDispatcher("/cart/showCart.jsp").forward(request,response);
        }
        else
        {
            request.getRequestDispatcher("/user/login.jsp").forward(request,response);
        }
    }

    public void deleteProductFromShopCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user= (User) request.getSession().getAttribute("user");
        int id= Integer.parseInt(request.getParameter("productid"));
        IProductService productService=new ProductServiceImpl();
        Product product=productService.findById(id);//获取产品
        ICartEntryDao cartEntryDao=new CartEntryDaoImpl();
        IShopCartService shopCartService=new ShopCartServiceImpl();
        shopCartService.deleteProduct(product,user.getId());
        request.getRequestDispatcher("/cart/showCart.do").forward(request,response);
    }

    public void changeProductNumber(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user= (User) request.getSession().getAttribute("user");
        int number= Integer.parseInt(request.getParameter("number"));
        System.out.println(number);
        int id= Integer.parseInt(request.getParameter("productid"));
        IProductService productService=new ProductServiceImpl();
        Product product=productService.findById(id);//获取产品
        IShopCartService shopCartService=new ShopCartServiceImpl();
        shopCartService.updateProduct(product,user.getId(),number);
        request.getRequestDispatcher("/cart/showCart.do").forward(request,response);
    }
}
